package com.warm.orm.eq.sys.mapper.impl;

import java.util.List;

import com.easy.query.core.enums.SQLExecuteStrategyEnum;
import com.warm.orm.domain.sys.SysRoleMenu;
import com.warm.orm.domain.sys.SysUser;
import com.warm.orm.eq.sys.mapper.SysMenuMapper;
import org.noear.solon.annotation.Component;

import com.easy.query.api4j.client.EasyQuery;
import com.easy.query.solon.annotation.Db;
import com.warm.orm.domain.sys.SysMenu;

/**
 * @author golly_qidajb7
 * @date 2023/11/03
 */
@Component
public class SysMenuMapperImpl implements SysMenuMapper {

    @Db
    private EasyQuery easyQuery;

    /* (non-Javadoc)
     * @see mapper.com.warm.orm.eq.sys.SysMenuMapper#checkMenuNameUnique(java.lang.String, java.lang.Long)
     */
    @Override
    public SysMenu checkMenuNameUnique(String menuName, Long parentId) {
        // TODO Auto-generated method stub
        return null;
    }

    /* (non-Javadoc)
     * @see mapper.com.warm.orm.eq.sys.SysMenuMapper#deleteMenuById(java.lang.Long)
     */
    @Override
    public int deleteMenuById(Long menuId) {
        return (int) this.easyQuery.deletable(SysMenu.class).whereById(menuId).executeRows();
    }

    /* (non-Javadoc)
     * @see mapper.com.warm.orm.eq.sys.SysMenuMapper#hasChildByMenuId(java.lang.Long)
     */
    @Override
    public int hasChildByMenuId(Long menuId) {
        // TODO Auto-generated method stub
        return 0;
    }

    /* (non-Javadoc)
     * @see mapper.com.warm.orm.eq.sys.SysMenuMapper#insertMenu(com.warm.select.api.orm.domain.sys.SysMenu)
     */
    @Override
    public int insertMenu(SysMenu menu) {
        return (int) this.easyQuery.insertable(menu).executeRows();
    }

    /* (non-Javadoc)
     * @see mapper.com.warm.orm.eq.sys.SysMenuMapper#selectMenuById(java.lang.Long)
     */
    @Override
    public SysMenu selectMenuById(Long menuId) {
        return this.easyQuery.queryable(SysMenu.class).whereById(menuId).firstOrNull();
    }

    /* (non-Javadoc)
     * @see mapper.com.warm.orm.eq.sys.SysMenuMapper#selectMenuList(com.warm.select.api.orm.domain.sys.SysMenu)
     */
    @Override
    public List<SysMenu> selectMenuList(SysMenu menu) {
        return this.easyQuery.queryable(SysMenu.class).toList();
    }

    /* (non-Javadoc)
     * @see mapper.com.warm.orm.eq.sys.SysMenuMapper#selectMenuListByRoleId(java.lang.Long, boolean)
     */
    @Override
    public List<Long> selectMenuListByRoleId(Long roleId, boolean menuCheckStrictly) {
        //        order by m.parent_id, m.order_num

        // from sys_menu m
        return this.easyQuery.queryable(SysMenu.class)
                // left join sys_role_menu rm on m.menu_id = rm.menu_id
                .leftJoin(SysRoleMenu.class, (t, t1) -> t1.eq(t, SysRoleMenu::getMenuId, SysMenu::getMenuId))
                // where rm.role_id = #{roleId}
                .where((t, t1) -> t1.eq(SysRoleMenu::getRoleId, roleId))
                //        <if test="menuCheckStrictly">
                //            and m.menu_id not in (select m.parent_id from sys_menu m inner join sys_role_menu rm on m.menu_id =
                //            rm.menu_id and rm.role_id = #{roleId})
                //        </if>
                .where(menuCheckStrictly, (t, t1) -> t1.notIn(SysRoleMenu::getRoleId,
                        this.easyQuery.queryable(SysMenu.class)
                                .innerJoin(SysRoleMenu.class, (t2, t3) -> t3.eq(t2, SysRoleMenu::getMenuId, SysMenu::getMenuId))
                                .where((t2, t3) -> t3.eq(SysRoleMenu::getRoleId, roleId))
                                .select(Long.class, s -> s.column(SysMenu::getMenuId)))
                )
                // select m.menu_id
                .select(Long.class, s -> s.column(SysMenu::getMenuId))
                .toList();
    }

    /* (non-Javadoc)
     * @see mapper.com.warm.orm.eq.sys.SysMenuMapper#selectMenuListByUserId(com.warm.select.api.orm.domain.sys.SysMenu)
     */
    @Override
    public List<SysMenu> selectMenuListByUserId(SysMenu menu) {
        return this.easyQuery.queryable(SysMenu.class).toList();
    }

    /* (non-Javadoc)
     * @see mapper.com.warm.orm.eq.sys.SysMenuMapper#selectMenuPerms()
     */
    @Override
    public List<String> selectMenuPerms() {
        // TODO Auto-generated method stub
        return null;
    }

    /* (non-Javadoc)
     * @see mapper.com.warm.orm.eq.sys.SysMenuMapper#selectMenuPermsByRoleId(java.lang.Long)
     */
    @Override
    public List<String> selectMenuPermsByRoleId(Long roleId) {
        // TODO Auto-generated method stub
        return null;
    }

    /* (non-Javadoc)
     * @see mapper.com.warm.orm.eq.sys.SysMenuMapper#selectMenuPermsByUserId(java.lang.Long)
     */
    @Override
    public List<String> selectMenuPermsByUserId(Long userId) {
        // TODO Auto-generated method stub
        return null;
    }

    /* (non-Javadoc)
     * @see mapper.com.warm.orm.eq.sys.SysMenuMapper#selectMenuTreeAll()
     */
    @Override
    public List<SysMenu> selectMenuTreeAll() {
        return this.easyQuery.queryable(SysMenu.class)
                .where(t -> t.in(SysMenu::getMenuType, new String[]{"M", "C"}))
                .toList()
                ;
    }

    /* (non-Javadoc)
     * @see mapper.com.warm.orm.eq.sys.SysMenuMapper#selectMenuTreeByUserId(java.lang.Long)
     */
    @Override
    public List<SysMenu> selectMenuTreeByUserId(Long userId) {
        // TODO Auto-generated method stub
        return null;
    }

    /* (non-Javadoc)
     * @see mapper.com.warm.orm.eq.sys.SysMenuMapper#updateMenu(com.warm.select.api.orm.domain.sys.SysMenu)
     */
    @Override
    public int updateMenu(SysMenu menu) {
        return (int) this.easyQuery.updatable(menu).setSQLStrategy(SQLExecuteStrategyEnum.ONLY_NOT_NULL_COLUMNS).executeRows();
    }

}
